package com.zip;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.zip.weather.ApiClient;
import com.zip.weather.Bean;
import com.zip.weather.WRetrofit;
import com.zip.weather.WeatherApi;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    String tag = "hms";
    private ZhuangbiApi mApi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mApi = Retrofitor.getRetrofit().create(ZhuangbiApi.class);
    }

    public void onClick(View view) {
        Weather();
        //RxJava();
    }

    public void Weather(){


        Observable observable = ApiClient.retrofit().create(WeatherApi.class).getWeather("101220602");
        Observer<Bean> observer = new Observer<Bean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Bean mainModel) {
                Log.e("mmm", "消息成功 22   " + mainModel.weatherinfo.toString());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);



//        Observable observable = ApiClient.retrofit().create(WeatherApi.class).getWeather("101220602");
//
//        Observer<Bean> observer = new Observer<Bean>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//            }
//
//            @Override
//            public void onNext(Bean bean) {
//                Log.e("aaa",bean.weatherinfo.toString());
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        };
//
//        observable.subscribe(observer);

    }

    //获取对象

    //获取列表
    private void RxJava() {
        mApi.search("110")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ZhuangbiImage>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(List<ZhuangbiImage> zhuangbiImages) {
                        for (ZhuangbiImage image : zhuangbiImages) {
                            Log.e("ccc", "recv   " + image.description);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}
